package com.itheima.service.mongo.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;

@Component
public class RedisIDService {

    @Autowired
    private RedisTemplate redisTemplate;

    public Long getNextId(String collectionName) {
        // 创建一个对象 long类型的原子自增器
        RedisAtomicLong redisAtomicLong = new RedisAtomicLong("id:" + collectionName, redisTemplate.getConnectionFactory());
        // 自增+1
        return redisAtomicLong.incrementAndGet();
    }
}
